/* Copyright (c) 2011 Vitria Technology, Inc.  All rights reserved. */
package com.vitria.m3oui.rest.app.mgr;

import java.io.FileInputStream;

import java.io.IOException;

import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import com.qilin.hammer.util.BaseRestService;
public class AppMemoryTest extends BaseRestService {
	public static String APP_ID = "/sla";
    public static String APP_URL_PREFIX = URL_PREFIX + "/app.mgr/apps";
    
    public static String INSTALL_UID = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAASLA";
    public static String UPGRADE_UID = "BBBBBBBB-AAAA-AAAA-AAAA-AAAAAAAAASLA";
    public static String EXPORT_UID =  "CCCCCCCC-AAAA-AAAA-AAAA-AAAAAAAAASLA";
    
    public static String INSTALL_W_UID = "WWWWWWWWWWWWWWWWWWW_A";
    public static String UPGRADE_W_UID = "WWWWWWWWWWWWWWWWWWW_B";
    public static String EXPORT_W_UID = "WWWWWWWWWWWWWWWWWWW_C";

    public AppMemoryTest(String name) {
        super(name);
    }
    
//    public void testLoop_install_enable_disable_unInstall() throws Exception {
//        String appID = APP_ID;
//        for (int i=1; i<=100; i++) {
//            setUp();
//            String url = APP_URL_PREFIX + appID + "?op=install" + "&uid=" + INSTALL_UID;
//            ClientResource resource = createSecureClientResource(url);
//            //resource.addQueryParameter("uid", "sample.zip");
//            Representation rep = resource.get();
//            String text = rep.getText();
//            System.out.println("loop: " + i);
//            System.out.print("install... ");
//            
//            resource = createSecureClientResource(APP_URL_PREFIX);
//            rep = resource.get();
//            text = rep.getText();
//            System.out.print("list... ");
//            
//            url = APP_URL_PREFIX + appID + "?op=enable" ;
//            resource = createSecureClientResource(url);
//            rep = resource.post(null);
//            text = rep.getText();
//            System.out.print("enable... ");
//            
//            url = APP_URL_PREFIX + appID + "?op=disable" ;
//            resource = createSecureClientResource(url);
//            rep = resource.post(null);
//            text = rep.getText();
//            System.out.print("disable... ");
//            
//            url = APP_URL_PREFIX + appID + "?op=uninstall" + "&keepAppConf=false" + "&keepUserData=false" ;
//            resource = createSecureClientResource(url);
//            rep = resource.get();
//            text = rep.getText();
//            System.out.println("uninstall!");
//            assertNotNull(text);
//            tearDown();
//        }
//    }

    public void testLoop_install_enable_disable_export_upgrade_unInstall_workbench() throws Exception {
        String appID = "/DBTaskApp";
        for (int i=1; i<=1000000; i++) {
            //setUp();
            
             //install SLA1.0 app 
           // String url = APP_URL_PREFIX + appID + "?op=install" + "&uid=" + INSTALL_UID;
           // ClientResource resource = createSecureClientResource(url);
            //resource.addQueryParameter("uid", "sample.zip");
           // Representation rep = resource.get();
           // String text  = rep.getText();
        	String url = APP_URL_PREFIX + appID+"?op=install&uid="+INSTALL_W_UID;
          	ClientResource resource = createSecureClientResource(url);
          	//resource.addQueryParameter("uid", "sample.zip");
          	Form form = new Form();
  			//form.add("op", "install");
  			//form.add("uid", INSTALL_UID);
  			form.add("title", "DBTaskApp");
  			form.add("description", "This is workbench app");
  			//form.add("version","1.1");
  			Representation rep = resource.post(form.getWebRepresentation());
  			String text  = rep.getText();
            System.out.println("loop: " + i);
            System.out.println("install... ");
            
            //list all app
            resource = createSecureClientResource(APP_URL_PREFIX);
            rep = resource.get();
            text = rep.getText();
            System.out.println("list... ");
            
            //enable SLA1.0 app
            url = APP_URL_PREFIX + appID + "?op=enable" ;
            resource = createSecureClientResource(url);
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("enable... ");

        	//disable SLA1.0 app
        	url = APP_URL_PREFIX + appID + "?op=disable" ;
            resource = createSecureClientResource(url);
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("disable... ");
            
            
            //export SLA1.0 app
            url = APP_URL_PREFIX + appID + "?op=export" + "&uid=" + EXPORT_W_UID ;
            resource = createSecureClientResource(url);
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("export...");
            
            //upgrade SLA2.0 app
            url = APP_URL_PREFIX + appID + "?op=upgrade" + "&uid=" + UPGRADE_W_UID + "&keepAppConf=false";
            resource = createSecureClientResource(url);
            //resource.addQueryParameter("uid", "sample.zip");
            form = new Form();
            form.add("title", "DBTaskApp");
  			form.add("description", "This is workbench app");
            rep = resource.post(form.getWebRepresentation());
            text = rep.getText();
            System.out.println("upgrade... ");
            
            //enable SLA1.0 app again
            url = APP_URL_PREFIX + appID + "?op=enable" ;
            resource = createSecureClientResource(url);
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("enable... ");
            
            //uninstall SLA2.0 app
            url = APP_URL_PREFIX + appID + "?op=uninstall" + "&keepAppConf=false";
            resource = createSecureClientResource(url);
            rep = resource.get();
            text = rep.getText();
            System.out.println("uninstall!");
            assertNotNull(text);
            //tearDown();
        }
    }
//    
//    public void testLoop_install_enable_disable_export_upgrade_unInstall() throws Exception {
//        String appID = APP_ID;
//        for (int i=1; i<=100000; i++) {
//            //setUp();
//            
//             //install SLA1.0 app 
//           // String url = APP_URL_PREFIX + appID + "?op=install" + "&uid=" + INSTALL_UID;
//           // ClientResource resource = createSecureClientResource(url);
//           //resource.addQueryParameter("uid", "sample.zip");
//           // Representation rep = resource.get();
//           // String text  = rep.getText();
//        	String url = APP_URL_PREFIX + appID+"?op=install&uid="+INSTALL_UID;
//          	ClientResource resource = createSecureClientResource(url);
//          	//resource.addQueryParameter("uid", "sample.zip");
//          	Form form = new Form();
//  			//form.add("op", "install");
//  			//form.add("uid", INSTALL_UID);
//  			form.add("title", "sla");
//  			form.add("description", "This is kpi builder");
//  			//form.add("version","1.1");
//  			Representation rep = resource.post(form.getWebRepresentation());
//  			String text  = rep.getText();
//            System.out.println("loop: " + i);
//            System.out.print("install... ");
//            
//            //list all app
//            resource = createSecureClientResource(APP_URL_PREFIX);
//            rep = resource.get();
//            text = rep.getText();
//            System.out.print("list... ");
//            
//            //enable SLA1.0 app
//            url = APP_URL_PREFIX + appID + "?op=enable" ;
//            resource = createSecureClientResource(url);
//            rep = resource.post(null);
//            text = rep.getText();
//            System.out.print("enable... ");
//
//        	//disable SLA1.0 app
//        	url = APP_URL_PREFIX + appID + "?op=disable" ;
//            resource = createSecureClientResource(url);
//            rep = resource.post(null);
//            text = rep.getText();
//            System.out.print("disable... ");
//            
//            
//            //export SLA1.0 app
//            url = APP_URL_PREFIX + appID + "?op=export" + "&uid=" + EXPORT_UID ;
//            resource = createSecureClientResource(url);
//            rep = resource.post(null);
//            text = rep.getText();
//            System.out.println("export...");
//         
//            //upgrade SLA2.0 app
//            url = APP_URL_PREFIX + appID + "?op=upgrade" + "&uid=" + UPGRADE_UID + "&keepAppConf=false" + "&keepUserData=false" ;
//            resource = createSecureClientResource(url);
//            //resource.addQueryParameter("uid", "sample.zip");
//    		form = new Form();
//    		form.add("title", "sla");
//    		form.add("description", "This is upgraded kpi builder");
//            rep = resource.post(form.getWebRepresentation());
//            text = rep.getText();
//            System.out.println("upgrade... ");
//            
//            //enable SLA1.0 app again
//            url = APP_URL_PREFIX + appID + "?op=enable" ;
//            resource = createSecureClientResource(url);
//            rep = resource.post(null);
//            text = rep.getText();
//            System.out.print("enable... ");
//            
//            //uninstall SLA2.0 app
//            url = APP_URL_PREFIX + appID + "?op=uninstall" + "&keepAppConf=false" + "&keepUserData=false" ;
//            resource = createSecureClientResource(url);
//            rep = resource.get();
//            text = rep.getText();
//            System.out.println("uninstall!");
//            assertNotNull(text);
//            //tearDown();
//        }
//    }
}
